/** 
 * TrackChooser.java
 * 
 * Peter Steiner and Fredrik Wallenius
 * DP08 - KTH - 2009
 * 
 */


package gameInitialization;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;


/**
 * Creates a dialog frame in which user can select what track to play.
 *
 */
public class TrackChooser extends JFrame{
	
	private JButton okButton, exitButton;
	private JList trackList;
	private DefaultListModel listModel;

	private static final long serialVersionUID = 1L;
	public static final int OK = 1;
	public static final int EXIT = -1;
	
	private int userChoice = -2;
	
	
	
	/**
	 * Initiates the dialog frame but will not show it. The size and text
	 * is hard coded.
	 */
	public TrackChooser(){
		super("Choose a track to play..");
		
		this.setSize(new Dimension(500,500));
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setLocation(200, 100);
		
		this.setLayout(new BorderLayout());
		
		/*try {
			authors = ImageIO.read(new File("authors.png"));
		} catch (IOException e) {
			authors = null;
		}*/
		
		
		
		Container south = new Container();
		south.setLayout(new FlowLayout());
		okButton = new JButton();
		okButton.setText("Ok!");
		south.add(okButton);
		exitButton = new JButton();
		exitButton.setText("Exit");
		south.add(exitButton);
		
		Container north = new Container();
		north.setLayout(new FlowLayout());
		north.setPreferredSize(new Dimension(500,220));
		
		JPanel center = new centerPanel();
		
		
			
		listModel = new DefaultListModel();
		trackList = new JList(listModel);
		trackList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		trackList.setPreferredSize(new Dimension(300,200));
		Border black = BorderFactory.createLineBorder(Color.black);
		trackList.setBorder(black);
		
		
		north.add(trackList);
		
		okButton.addActionListener(new ButtonListener(TrackChooser.OK));
		exitButton.addActionListener(new ButtonListener(TrackChooser.EXIT));
		
		this.getContentPane().add(north, BorderLayout.NORTH);
		this.getContentPane().add(south, BorderLayout.SOUTH);
		this.getContentPane().add(center, BorderLayout.CENTER);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.pack();
		this.setVisible(true);	
		
	}
	
	/**
	 * 
	 * @param trackName	A string with one tracks name.
	 */
	public void addTrackToList(String trackName){
		listModel.addElement(trackName);
		return;
	}
	

	/**
	 * Display the allready created windows and does not return until the user has
	 * either chosen one track or clicked exit.
	 * 
	 * @return	The index of the chosen track, 0..*
	 */
	public int getTrack(){
		this.setVisible(true);
		
		do{
			try {
				Thread.sleep(50);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(userChoice>-2){
				this.setVisible(false);
				if(userChoice == TrackChooser.EXIT){
					userChoice = -2;
					return TrackChooser.EXIT;
				}
					
				userChoice = -2;
				return trackList.getSelectedIndex();
			}
			
			
		}while(true);
		
		
		
	}
	
	private class ButtonListener implements ActionListener{
		
		private int whatHappened = 0;
		
		public ButtonListener (int whatHappened_){
			whatHappened = whatHappened_;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) {
			if(whatHappened == TrackChooser.OK){
				if(trackList.getSelectedIndex() == -1) //User did not select a track
					return;}

			userChoice = whatHappened;
			//System.out.println(whatHappened);
			
		}
		
	}
	
	protected class centerPanel extends JPanel{
		ImageIcon authors;
		
		public centerPanel(){
			authors = new ImageIcon("authors.png");
			
			this.setLayout(new FlowLayout());
			this.setPreferredSize(new Dimension(500,110));
		}
		
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			authors.paintIcon(this, g, 55, 0);
			
		}
		
		
	}

	
	

}